<template>
  <div ref="chartWraper" :style="{ height: height }" />
</template>

<script>
// import * as echarts from "echarts";
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'BaseChart',
  mixins: [resize],
  props: {
    height: {
      type: String,
      default: '100%'
    },
    type: {
      type: String,
      default: 'bar'
    },
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  // watch: {
  //   data: {
  //     deep: true,
  //     immediate: true,
  //     handler() {
  //       this.initChart();
  //     },
  //   },
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    resize() {
      this.chart.resize()
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'chartWraper')
      this.$nextTick(() => {
        this.chart.setOption({
          title: {
            top: '10%',
            text: '项目进度统计表',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '10%',
            orient: 'vertical',
            left: 'left'
          },
          color: ['#C1EBDD', '#FFC851', '#5A5476', '#1869A0', '#FF9393'],
          series: [
            {
              type: 'pie',
              radius: '50%',
              data: this.chartData,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0
                }
              }
            }
          ]
        })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
